package org.gten.gegedexin.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.gten.gegedexin.dao.UserDao;
import org.gten.gegedexin.pojo.MailInfoPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReadMailList {
    @Autowired
    MailInfoServiceImpl mailInfoService;

    String user;
    //  总的邮件列表
    JSONArray jsonArray = new JSONArray();
    //  回收站邮件列表
    JSONArray recycleArray = new JSONArray();
    //  已读列表
    JSONArray readJsonArray = new JSONArray();
    //  未读列表
    JSONArray notReadJsonArray = new JSONArray();
    //  收藏列表
    JSONArray collectJsonArray = new JSONArray();

    //  工作列表
    JSONArray working = new JSONArray();

    public void readList(String receiverID){
        //  根据收件人的id，从数据库中查找所有它收到的邮件
        //  将邮件列表发送给收件人，列表项包括：邮件id，发件人，标题，时间，邮件状态(已读或未读)
        JSONObject json = new JSONObject();
        List<MailInfoPojo> mailList = mailInfoService.getMailInfoByReceiver(receiverID);
        for(MailInfoPojo mail:mailList) {
            json.put("id", String.format("%08d", mail.getMailId()));
            json.put("sender", mail.getSender());
            json.put("title", mail.getHeader());
            json.put("date", mail.getDate());
            json.put("state", mail.getIsRead());

            //  若该邮件未被删除，则加入josnArray中，否则加入recycleArray中
            if (mail.getIsRead() != 2){
                jsonArray.add(json);
                if (mail.getIsRead() == 0)
                    notReadJsonArray.add(json);
                else if(mail.getIsRead()==1)
                    readJsonArray.add(json);
            }
            else if (mail.getIsRead() == 2){
                Duration duration = Duration.between(mail.getDelDate(),LocalDateTime.now());
                if(duration.toDays()>=7)
                    mailInfoService.delMail(mail.getMailId());
                else {
                    json.put("delDate",mail.getDelDate().toString());
                    recycleArray.add(json);
                }
            }
        }
        settingWorking(4);
    }

    //  返回指定页的邮件列表
    public JSONArray readPage(int page){
        JSONArray jsonPage = new JSONArray();
        for(int i=0+(page-1)*10;i<page*10;i++){
            if(i>=jsonArray.size())break;
            jsonPage.add(jsonArray.get(i));
        }
        JSONObject numJSON = new JSONObject();
        numJSON.put("totalcount",jsonArray.size());
        jsonPage.add(numJSON);
        return jsonPage;
    }

    //  返回指定页的邮件列表
    public JSONArray recyclePage(int page){
        JSONArray jsonPage = new JSONArray();
        for(int i=0+(page-1)*10;i<page*10;i++){
            if(i>=recycleArray.size())break;
            jsonPage.add(recycleArray.get(i));
        }
        JSONObject numJSON = new JSONObject();
        numJSON.put("totalcount",recycleArray.size());
        jsonPage.add(numJSON);
        return jsonPage;
    }

    //  返回邮件列表页数
    public int returnRecyclePage(){
        return (recycleArray.size()/10) + 1;
    }

    public JSONArray getJsonArray() {
        return jsonArray;
    }

    public JSONArray getworking(){
        return working;
    }

    public void setUser(String userID){user = userID;}
    public String getUser(){return user;}

    public void settingWorking(int state){
        switch(state){
            case 0:
                working = readJsonArray;
                break;
            case 1:
                working = notReadJsonArray;
                break;
            case 2:
                working = recycleArray;
                break;
            case 3:
                working = collectJsonArray;
                break;
            case 4:
                working = jsonArray;
                break;
            default:
                working = jsonArray;
                break;
        }
    }
}
